﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Client.CommonUI;
using Zwms.Data.CommonObject;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.InfoWms
{
    public partial class LocationForm : BaseForm
    {
        #region 成员变量
        public WhLocation CurrentRecord { get; set; }
        private EnumOpenType OpenType { get; set; }
        #endregion

        #region 构造函数
        public LocationForm(EnumOpenType opentype, string sLocationCode, string sWarehouseCode)
        {
            InitializeComponent();
            OpenType = opentype;

            if (opentype == EnumOpenType.New)
            {
                CurrentRecord = new WhLocation();
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbLocationCode.ReadOnly = true;
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client =
                    new ServiceReferenceZwms.ServiceZwmsClient();
                WhLocation record = new WhLocation();
                record.LocationCode = sLocationCode;
                record.WarehouseCode = sWarehouseCode;
                CurrentRecord = client.QueryLocation(record);
            }
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
        }

        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentRecord);
            }
        }

        private void LoadInfo(WhLocation record)
        {
            this.tbLocationCode.Text = record.LocationCode;
            this.tbLocationBarCode.Text = record.LocationCode;
            this.tbLocationName.Text = record.LocationName;
            this.cmbLocationType.SelectedIndex =
                this.cmbLocationType.Items.IndexOf(record.LocationType);
            this.tbsArea.SetTextBoxContent(record.AreaName);
            this.tbsArea.Tag = record.AreaCode;
            this.tbWarehouse.Text = record.WarehouseName;
            this.tbWarehouse.Tag = record.WarehouseCode;
            this.tbsShelf.SetTextBoxContent(record.ShelfName);
            this.tbsShelf.Tag = record.ShelfCode;
            this.tbPosition.Text = record.ShelfPosition;
            this.tbsStoreGoods.SetTextBoxContent(record.StoreGoodsName);
            this.tbsStoreGoods.Tag = record.StoreGoodsCode;
            this.tbRemark.Text = record.Remark;

            //------------------------------------------------------
           ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
            DateTime dtNow = client1.GetCurrentSystemTime();

            this.tbCreateUser.Text = record.CreateUserName;
            this.dpuCreateDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.CreateDate + record.CreateTime));
            this.tbEditUser.Text = record.EditUserName;
            this.dpuEditDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.EditDate + record.EditTime));

        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.OpenType == EnumOpenType.New)
            {
                if (tbLocationCode.Text.Trim() == "")
                {
                    bRet = false;
                    this.tbLocationCode.BackColor = Color.Yellow;
                }
            }

            if (tbLocationBarCode.Text.Trim() == "")
            {
                bRet = false;
                this.tbLocationBarCode.BackColor = Color.Yellow;
            }

            //if (tbsArea.GetTextBoxContent() == "")
            //{
            //    bRet = false;
            //    this.tbsArea.SetTextBGColor(Color.Yellow);
            //}

            if (tbWarehouse.Text == "")
            {
                bRet = false;
                this.tbWarehouse.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要添加（修改）库位信息吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {

                WhLocation record = new WhLocation();
                record.LocationCode = tbLocationCode.Text;
                record.BarCode = tbLocationBarCode.Text;
                record.LocationName = tbLocationName.Text;
                if (cmbLocationType.SelectedIndex != -1)
                {
                    record.LocationType = cmbLocationType.SelectedItem.ToString();
                } 

                if (tbsArea.Tag != null)
                {
                    record.AreaName = tbsArea.GetTextBoxContent();
                    record.AreaCode = tbsArea.Tag.ToString();
                }
                else
                {
                    record.AreaName = "";
                    record.AreaName = "";
                }
                
                record.WarehouseName = tbWarehouse.Text;
                record.WarehouseCode = tbWarehouse.Tag.ToString();

                if (tbsShelf.Tag != null)
                {
                    record.ShelfName = tbsShelf.GetTextBoxContent();
                    record.ShelfCode = tbsShelf.Tag.ToString();
                }
                else
                {
                    record.ShelfName = "";
                    record.ShelfName = "";
                }
                if (string.IsNullOrWhiteSpace(tbsStoreGoods.GetTextBoxContent()))
                {
                    record.StoreGoodsCode = tbsStoreGoods.Tag.ToString();
                    record.StoreGoodsName = tbsStoreGoods.GetTextBoxContent();
                }
                record.ShelfPosition = tbPosition.Text;
                if (tbsStoreGoods.Tag != null)
                {
                    record.StoreGoodsName = tbsStoreGoods.GetTextBoxContent();
                    record.StoreGoodsCode = tbsStoreGoods.Tag.ToString();
                }
                record.Remark = tbRemark.Text;

                //------------------------------------
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                if (this.OpenType == EnumOpenType.New)
                {
                    record.CreateUserName = MainForm.CurrentUser.UserName;
                    record.CreateUserCode = MainForm.CurrentUser.Id;
                    record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                    record.EditUserName = "";
                    record.EditUserCode = "";
                    record.EditDate = "";
                    record.EditTime = "";
                }
                else
                {
                    record.EditUserName = MainForm.CurrentUser.UserName;
                    record.EditUserCode = MainForm.CurrentUser.Id;
                    record.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
                }

                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();

                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client.AddLocation(record);
                }
                else
                {
                    bRet = client.UpdateLocation(record);
                }
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.OpenType == EnumOpenType.New)
            {
                
                tbLocationCode.Text = "";
            }

            tbLocationName.Text = "";
            tbLocationBarCode.Text = "";
            cmbLocationType.SelectedIndex = -1;
            tbsArea.SetTextBoxContent( "");
            tbsArea.Tag = "";
            tbWarehouse.Text = "";
            tbWarehouse.Tag = "";
            tbsShelf.SetTextBoxContent("");
            tbPosition.Text = "";
            tbsShelf.Tag = "";
            tbRemark.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }

        private void tbsArea_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.AreaWnd uc = new AreaWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "库区选择");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
                tbsArea.Tag = uc.SelectedRecord.AreaCode;

                tbWarehouse.Text = uc.SelectedRecord.WarehouseName;
                tbWarehouse.Tag = uc.SelectedRecord.WarehouseCode;
            }
        }

        private void tbsShelf_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.ShelfWnd uc = new ShelfWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "货架选择");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsShelf.SetTextBoxContent(uc.SelectedRecord.ShelfName);
                tbsShelf.Tag = uc.SelectedRecord.ShelfCode;
            }
        }

        private void tbsStoreGoods_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.GoodsWnd uc = new GoodsWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "货架选择");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsStoreGoods.SetTextBoxContent(uc.SelectedRecord.GoodsName);
                tbsStoreGoods.Tag = uc.SelectedRecord.Id;
            }
        }
        #endregion
    }
}
